import CryptoJS from "./crypto.js"

function formatTime(time) {
	if (typeof time !== 'number' || time < 0) {
		return time
	}
	var hour = parseInt(time / 3600)
	time = time % 3600
	var minute = parseInt(time / 60)
	time = time % 60
	var second = time

	return ([hour, minute, second]).map(function(n) {
		n = n.toString()
		return n[1] ? n : '0' + n
	}).join(':')
}

function formatLocation(longitude, latitude) {
	if (typeof longitude === 'string' && typeof latitude === 'string') {
		longitude = parseFloat(longitude)
		latitude = parseFloat(latitude)
	}

	longitude = longitude.toFixed(2)
	latitude = latitude.toFixed(2)

	return {
		longitude: longitude.toString().split('.'),
		latitude: latitude.toString().split('.')
	}
}
var dateUtils = {
	UNITS: {
		'年': 31557600000,
		'月': 2629800000,
		'天': 86400000,
		'小时': 3600000,
		'分钟': 60000,
		'秒': 1000
	},
	humanize: function(milliseconds) {
		var humanize = '';
		for (var key in this.UNITS) {
			if (milliseconds >= this.UNITS[key]) {
				humanize = Math.floor(milliseconds / this.UNITS[key]) + key + '前';
				break;
			}
		}
		return humanize || '刚刚';
	},
	format: function(dateStr) {
		var date = this.parse(dateStr)
		var diff = Date.now() - date.getTime();
		if (diff < this.UNITS['天']) {
			return this.humanize(diff);
		}
		var _format = function(number) {
			return (number < 10 ? ('0' + number) : number);
		};
		return date.getFullYear() + '/' + _format(date.getMonth() + 1) + '/' + _format(date.getDate()) + '-' +
			_format(date.getHours()) + ':' + _format(date.getMinutes());
	},
	parse: function(str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串，转化为一个Date对象
		var a = str.split(/[^0-9]/);
		return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
	}
}
function getUrlparas(url){
	if(url.indexOf('&amp;')>-1){
		url =url.replace('amp;','');
	}
	let paraString = url.substring(url.indexOf("?")+1,url.length).split("&");
	let paraObj = {};
	let j = null;
	for (let i=0;j=paraString[i]; i++){
		paraObj[j.substring(0,j.indexOf("="))] = j.substring(j.indexOf("=")+1,j.length);  
	}
	return paraObj; 
}
function newGuid() {  //uuid
	let s4 = function() {
		return (65536 * (1 + Math.random()) | 0).toString(16).substring(1);
	}
	return (s4() + s4() + s4() + "4" + s4().substr(0, 3) + s4() + s4() + s4() + s4()).toUpperCase();
}
const authKey = "nHAcrwnrbURHgX82SDCq";
function authVideo(str){
	let _str = str.replace(/^(http:|https:)\/\/[^/]+/, "");
	const _time = parseInt(new Date().getTime()/1000)+7200;
	const uuid = newGuid();
	_str = _str+'-'+_time+'-'+uuid+'-0-'+authKey;
	let HashValue = CryptoJS.MD5(_str).toString();
	let authUrl = str+'?auth_key='+_time+'-'+uuid+'-0-'+HashValue;
	return authUrl;
}

// 判断类型
const gettype = Object.prototype.toString;
const isObj = function(o) {
	return gettype.call(o) === "[object Object]";
};

const isArray = function(o) {
	return gettype.call(o) === "[object Array]";
};

const isNull = function(o) {
	return gettype.call(o) === "[object Null]";
};

const isBoolean = function(o) {
	return gettype.call(o) === '[object Boolean]'
}
/**
 * 请求地址参数转字符串
 * @param {string} url
 * @param {object} data 参数对象
 * @param {string} prefix 前缀
 */
const url2String = function(url, data, prefix) {
	let query = [];
	Object.keys(data).forEach(item => {
		query.push(`${item}=${data[item]}`);
	});
	return `${prefix}${url}?${query.join("&")}`;
};
export {
	formatTime,
	formatLocation,
	dateUtils,
	getUrlparas,
	newGuid,
	authVideo,
	isBoolean,
	url2String
}
